package com.caissa.framework.starter.autoconfigure.annotation;

import com.caissa.ecp.base.api.common.exception.CommonRuntimeException;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;

import java.io.IOException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.concurrent.ExecutionException;

public class ExceptionTransformer {

    public static void ifThrowCheckedException(Throwable e) throws MethodArgumentNotValidException, MissingServletRequestParameterException, SQLIntegrityConstraintViolationException, IOException, ExecutionException, InterruptedException {
        if (e instanceof CommonRuntimeException) {
            CommonRuntimeException commonRuntimeException = (CommonRuntimeException)e;
            throw commonRuntimeException;
        }else if(e instanceof MethodArgumentNotValidException){
            MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException)e;
            throw methodArgumentNotValidException;
        }else if(e instanceof SecurityException){
            SecurityException securityException = (SecurityException)e;
            throw securityException;
        }else if(e instanceof IllegalArgumentException){
            IllegalArgumentException illegalArgumentException = (IllegalArgumentException)e;
            throw illegalArgumentException;
//        }else if(e instanceof PersistenceException){
//            PersistenceException persistenceException = (PersistenceException)e;
//            throw methodArgumentNotValidException;
        }else if(e instanceof MissingServletRequestParameterException){
            MissingServletRequestParameterException missingServletRequestParameterException = (MissingServletRequestParameterException)e;
            throw missingServletRequestParameterException;
        }else if(e instanceof SQLIntegrityConstraintViolationException){
            SQLIntegrityConstraintViolationException sQLIntegrityConstraintViolationException = (SQLIntegrityConstraintViolationException)e;
            throw sQLIntegrityConstraintViolationException;
        }else if(e instanceof UnrecognizedPropertyException){
            UnrecognizedPropertyException unrecognizedPropertyException = (UnrecognizedPropertyException)e;
            throw unrecognizedPropertyException;
        }else if(e instanceof IOException){
            IOException iOException = (IOException)e;
            throw iOException;
        }else if(e instanceof InterruptedException){
            InterruptedException interruptedException = (InterruptedException)e;
            throw interruptedException;
        }else if(e instanceof ExecutionException){
            ExecutionException executionException = (ExecutionException)e;
            throw executionException;
        }
    }
}
